package helper.com.controller;

import helper.com.exception.CustomException;
import helper.com.res.AjaxResponse;
import helper.com.service.LogService;
import helper.com.utils.TimeUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.weaver.loadtime.Aj;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;

/**
 * 动态改变日志配置接口
 * @author 大忽悠
 * @create 2022/5/17 14:33
 */
@RestController
@RequestMapping("/log")
@RequiredArgsConstructor
@Slf4j
public class LogController {
    private final LogService logService;

    @ResponseBody
    @PutMapping("/level")
    public AjaxResponse changeLogLevel(@RequestParam String level,@RequestParam(required = false) String loggerName){
        log.info("请求将{}日志等级改为{},当前时间为: {}",(loggerName==null||loggerName.equals(""))?"rootLogger":loggerName,level, TimeUtil.getCurTime());
        return logService.changeLogLevel(level,loggerName);
    }

    @ResponseBody
    @PutMapping("/file")
    public AjaxResponse changeLogFile(@RequestParam String appenderName,@RequestParam String logDir){
        log.info("请求将{}【appender】的日志存放路径改为:{}",appenderName,logDir);
        return logService.changeLogFile(appenderName,logDir);
    }

    @GetMapping("/download")
    public void downLoadLogFile(HttpServletResponse response){
        log.info("请求下载日志文件");
        logService.downLoadLogFile(response);
    }

    @ResponseBody
    @GetMapping("/test_change")
    public AjaxResponse testChange(){
        log.error("error");
        log.warn("warn");
        log.info("info");
        log.debug("debug");
        log.trace("trace");
        log.error("错误:",new CustomException());
        return AjaxResponse.success();
    }
}
